﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="cache-control" content="no-cache"> 
    <title>流程中心</title>
    <!-- jQuery -->
    <script src="WFRes/_Scripts/jquery/jquery.js?v=1.12.4"></script>
    <script src="http://g.alicdn.com/dingding/dingtalk-pc-api/2.3.1/index.js"></script>
    <script>
        //钉钉、微信打开消息
        var url = "";
        loginform = "";
        var getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.replace("__", "&").substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        };
        url = window.location.toString().replace("__", "&");
        loginform = getUrlParam("loginfrom");
        var state = getUrlParam("state");
        var code = getUrlParam("code");
        var workItemID = getUrlParam("WorkItemID");

        var LoginForDingTalk = function () {
            $.ajax({
                url: "Mobile/DingTalk/ValidateLoginForDingTalk",
                data: {
                    state: state,
                    code: code
                },
                async: false,
                success: function (result) {
                    Redirect();
                }
            });
        }

        var LoginForWechat = function (state, code) {
            $.ajax({
                url: "WeChat/ValidateLoginForWeChat",
                data: {
                    state: state,
                    code: code
                },
                async: false,
                success: function (res) {
                    Redirect();
                }
            })
        }
        // 重定向
        var Redirect = function () {
            if(workItemID){
                //获取URL参数
                params = getUrlVars();
                //update by ouyangsk 增加推送标识符，不允许移动端再次打开已经转办的推送表单
                params.mobileNoticeFlag = true;
                var paramString = JSON.stringify(params);
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "WorkItemSheets/WorkItemSheets",
                    data: { paramString: paramString },
                    dataType: "json",
                    success: function (data) {
                        if (data.Success) {
                            window.location.href = data.Message + "&loginfrom=dingtalk&source=message";
                        }
                    },
                    error: function (data) {

                    }
                });
            }else {
                //非钉钉消息，直接跳转到待办界面
                window.top.location.href = "./#/app/Workflow/MyUnfinishedWorkItem";
            }
        }
        var getUrlVars = function () {
            var vars = {};
            var hash;
            var hashs = window.location.href.replace("__", "&").slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashs.length; i++) {
                hash = hashs[i].split('=');
                vars[hash[0]] = hash[1];
            }
            return vars;
        }
        //根据URL参数判断是否钉钉登录
        if (loginform == "dingtalk") {
            LoginForDingTalk(url);
        } else {
            LoginForWechat(state, code);
        }
    </script>
</head>
<body>

</body>
</html>
